package com.fuhe.chen.vendingmachine.controller.admin;

import com.fuhe.chen.vendingmachine.anno.AddLog;
import com.fuhe.chen.vendingmachine.dto.ApiResponse;
import com.fuhe.chen.vendingmachine.common.Utils;
import com.fuhe.chen.vendingmachine.constant.MessageConstant;
import com.fuhe.chen.vendingmachine.dto.cond.OrderCond;
import com.fuhe.chen.vendingmachine.pojo.Order;
import com.fuhe.chen.vendingmachine.service.IOrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 账单管理首页
 * 1.账单查询
 */
@Controller
@Api(tags = "账单管理")
@RequestMapping("/admin/order")
public class OrderController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    IOrderService orderService;

    /**
     * 账单管理查询/翻页
     */
    @ResponseBody
    @ApiOperation("账单管理查询/翻页")
    @PostMapping({"/query", "/query/page/{page}"})
    public ApiResponse<PageInfo<Order>> turnPage(
            @PathVariable(name = "page")
                    Integer page,
            @RequestParam(name = "orderStatus", required = false)
                    Integer orderStatus,
            @RequestParam(name = "payMethod", required = false)
                    Integer payMethod,
            @RequestParam(name = "start", required = false)
                    Long start,
            @RequestParam(name = "end", required = false)
                    Long end,
            @RequestParam(name = "trade_no", required = false)
                    String trade_no
    ) {
        //添加查询条件
        OrderCond orderCond = new OrderCond();
        orderCond.setOrderStatus(orderStatus);
        orderCond.setPayMethod(payMethod);
        orderCond.setStart(start);
        orderCond.setEnd(end);
        orderCond.setTrade_no(trade_no);

        PageInfo<Order> orders = orderService.findByCondition(orderCond, page, 10);

        ApiResponse<PageInfo<Order>> response = new ApiResponse<>();
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setSuccess(true);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setData(orders);
        return response;
    }

    /**
     * 查询账单详情
     */
    @ResponseBody
    @ApiOperation("根据账单id查询账单详情")
    @GetMapping("/detail/{orderId}")
    public ApiResponse<Order> orderDetail(@PathVariable String orderId) {
        Order order = orderService.queryOrder(orderId);

        ApiResponse<Order> response = new ApiResponse<>();
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setSuccess(true);
        response.setMessage(MessageConstant.Success.QUERY_SUCCESS_MESSAGE);
        response.setData(order);
        return response;
    }

    /**
     * 批量删除账单
     * @param ids
     * @return
     */
    @ResponseBody
    @ApiOperation("根据多个id删除多个账单,每个id间以','隔开")
    @PostMapping("/delAll")
    @AddLog(operation = "批量删除账单")
    public ApiResponse<String> delAll(@RequestParam(required = true) String ids) {

        List list = Utils.getOrders(ids);
        orderService.delAll(list);

        ApiResponse<String> response = new ApiResponse<>();
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setSuccess(true);
        response.setMessage(MessageConstant.Success.DELETE_SUCCESS_MESSAGE);
        response.setData("success");
        return response;
    }

    @ResponseBody
    @ApiOperation("根据账单id删除订单信息")
    @PostMapping("/delete")
    @AddLog(operation = "根据账单id删除订单信息")
    public ApiResponse<String> delete(@RequestParam(name = "orderId", required = true) String orderId) {

        orderService.delete(orderId);

        ApiResponse<String> response = new ApiResponse<>();
        response.setCode(MessageConstant.Success.SUCCESS_CODE);
        response.setSuccess(true);
        response.setMessage(MessageConstant.Success.DELETE_SUCCESS_MESSAGE);
        response.setData("success");
        return response;
    }
}
